// pages/index/swiper/swiper.js
import {
  list
} from '../../../../api/ad'
import {
  getInfo
} from '../../../../api/mch'
const util = require('../../../../utils/util')
let app = getApp();
Component({

  // 以下是旧式的定义方式，可以保持对 <2.2.3 版本基础库的兼容
  attached: function() {

    // 在组件实例进入页面节点树时执行

  },
  detached: function() {
    // 在组件实例被从页面节点树移除时执行
  },
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
    }
  },
  lifetimes: {
    attached() {
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
        this.setData({
          tagIds: this.data.tagIds
        })
      
      this.setNavSize()
      this.loadAd()
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tagIds: [],
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    photoList: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 500,
    previousMargin: 0,
    color: [],
    c: '',
    nextMargin: 0,
    pageNum: 0,
    pageSize: 10,
    startSwipers: false,
    transparent: 1,
    headerColor: 'ragb(255,255,255,0)',
    mch:{logo:'',name:''}
  },

  /**
   * 组件的方法列表
   */
  created() {
    this.loadMch();
  },
  methods: {
    toLink(e) {
            if (e.currentTarget.dataset.type.hasOwnProperty('typeId') && !util.isNull(e.currentTarget.dataset.type.typeId)) {
      if (e.currentTarget.dataset.type.type === 0) {
        wx.navigateTo({
          url: '/pages/goods/product/details?id=' + e.currentTarget.dataset.type.typeId
        })
      }else if (e.currentTarget.dataset.type.type === 9) {
        wx.navigateTo({
          url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.type.typeId
        })
      } else if (e.currentTarget.dataset.type.type === 2) {
        wx.navigateTo({
          url:'/pages/member/detail/detail?id='+e.currentTarget.dataset.type.typeId,
        })
      } else if (e.currentTarget.dataset.type.type === 1) {
        wx.navigateTo({
          url: '/pages/goods/category/category?id=' + e.currentTarget.dataset.type.typeId
        })
      }  
    }
    },
    searchClick() {
      wx.navigateTo({
        url: '/pages/goods/search/index'
      })
    },
    changeBackground(e) {
      this.setData({
        c: this.data.color[e.detail.current]
      })
    },
    detailsClick() {
      wx.navigateTo({
        url: '/pages/goods/product/details',
      })
    },
    setNavSize: function() {
      var that = this,
        sysinfo = wx.getSystemInfoSync(),
        statusHeight = sysinfo.statusBarHeight,
        isiOS = sysinfo.system.indexOf('iOS') > -1,
        navHeight;
      console.log(sysinfo)
      if (!isiOS) {
        navHeight = 48;
      } else {
        navHeight = 44;
      }
      that.setData({
        status: statusHeight,
        navHeight: navHeight
      })
    },
    loadMch() {
      var _this = this;
      getInfo(app.globalData.mchId).then(res => {
        _this.data.mch = res.data;
        _this.setData({
          mch:_this.data.mch
        })
      });
    },
    loadAd() {
      var _this = this
      list({
        mchId: app.globalData.mchId,
        tagIds: this.data.tagIds
      }).then(res => {
        res.data.data.forEach(element => {
          element.backgroundColor = "linear-gradient(180deg," + element.backgroundColor + ", rgba(248, 248, 248, 1))"
          _this.data.color.push(element.backgroundColor)
          element.thumbnail = util.thumbnail(element.thumbnail, 710, 355)
          this.data.c = this.data.color[0]

        });
        _this.data.photoList = res.data.data
        this.setData({
          color: this.data.color,
          photoList: _this.data.photoList,
          c: this.data.c,
          statusBarHeight: app.globalData.statusBarHeight,
        });
      });
    },
    // 头部改变
    resetClick() {
      var _this = this
      var timer = setTimeout(function() {
        _this.data.transparent = '1'
        _this.data.headerColor = 'rgba(255,255,255,0)'
        _this.setData({
          transparent: _this.data.transparent,
          headerColor: _this.data.headerColor
        })
        console.log('22222')
        clearTimeout(timer)
      }, 1)

    },
    startClick() {
      this.data.startSwipers = !this.data.startSwipers
      // this.data.transparent = '0.9'
      // this.data.headerColor = 'rgba(255,255,255,0)'
      this.setData({
        startSwipers: this.data.startSwipers,
        // transparent: this.data.transparent,
        // headerColor: this.data.headerColor
      })
    },
    secondClick() {
      this.data.transparent = '0.7'
      this.data.headerColor = 'rgba(255,255,255,0.2)'
      this.setData({
        transparent: this.data.transparent,
        headerColor: this.data.headerColor
      })
    },
    thirdClick() {
      this.data.transparent = '0.5'
      this.data.headerColor = 'rgba(255,255,255,0.5)'
      this.setData({
        transparent: this.data.transparent,
        headerColor: this.data.headerColor
      })
    },
    fourthClick() {
      this.data.transparent = '0.3'
      this.data.headerColor = 'rgba(255,255,255,0.7)'
      this.setData({
        transparent: this.data.transparent,
        headerColor: this.data.headerColor
      })
    },
    fifthClick() {
      this.data.transparent = '0'
      this.data.headerColor = 'rgba(255,255,255,0.9)'
      this.setData({
        transparent: this.data.transparent,
        headerColor: this.data.headerColor
      })
    }
  }
})